package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.SysUser;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Classname MenuController
 * @Description TODO    菜单控制器
 * @Date 2019/12/6
 * @Created by Administrator
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    MenuService menuService;

    /**
     * 在弹窗中展示菜单列表
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Menu> menuList=menuService.findAll();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }

    /**
     * 菜单管理-新增菜单
     * @param menu
     * @return
     */
    @RequestMapping("/save")
    @PreAuthorize("hasAuthority('MENU_ADD')")
    public Result save(@RequestBody Menu menu) {

        try {
            menuService.save(menu);
            return new Result(true,MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MENU_FAIL);
        }
    }

    /**
     * 权限管理-分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return menuService.queryPage(queryPageBean);
    }

    /**
     * 权限管理-编辑信息的回显
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public Result findById(Integer id) {
        try {
            Menu menu=menuService.findById(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }

    /**
     * 权限管理-编辑信息
     * @param menu
     * @return
     */
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('MENU_EDIT')")
    public Result edit(@RequestBody Menu menu) {
        try {
            menuService.edit(menu);
            return new Result(true,MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MENU_FAIL);
        }
    }

    /**
     * 权限管理-删除权限
     * @param id
     * @return
     */
    @RequestMapping("/delById")
    @PreAuthorize("hasAuthority('MENU_DELETE')")
    public Result delById(Integer id) {
        try {
            menuService.delById(id);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        }catch (RuntimeException e){
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }
    }

    /**
     * 根据用户名查询菜单列表
     */
    @RequestMapping("/showMenu")
    public Result showMenu(String username){
        try {
            List<Menu> menuList = menuService.showMenu(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_MENU_FAIL);
        }

    }
}
